#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<stdlib.h>
#include<string.h>
int val=0;
struct Ret 
{
    int exitno;
    int exittime;
    //...
};
void* Routine(void* avgs)
{
    int cnt=1;
    while(1)
    {
        printf("I am %s... val:%d\n",(char*)avgs,val);
        sleep(1);
        cnt++;
        if(cnt==3)
        {
            struct Ret* p=(struct Ret*)malloc(sizeof(struct Ret));
            p->exitno=0;
            p->exittime=6666;
            pthread_exit((void*)p);
            //pthread_cancel(pthread_self());
        }    
    }

}
int main()
{
    pthread_t tid1,tid2,tid3;
    pthread_create(&tid1,NULL,Routine,(void*)"pthread 1");
    pthread_create(&tid2,NULL,Routine,(void*)"pthread 2");
    pthread_create(&tid3,NULL,Routine,(void*)"pthread 3");
    int cnt=0;
    while(1)
    {
        printf("I am main pthread...val:%d\n",val++);
        sleep(1);
        cnt++;
        if(cnt==3)
            break;
    }
    printf("wait for pthread...\n");
    void* ret;
    pthread_join(tid1,&ret);
    printf("pthread id:%x exitno:%d exittime:%d\n",tid1,((struct Ret*)ret)->exitno,((struct Ret*)ret)->exittime);
    pthread_join(tid2,&ret);
    printf("pthread id:%x exitno:%d exittime:%d\n",tid2,((struct Ret*)ret)->exitno,((struct Ret*)ret)->exittime);
    pthread_join(tid3,&ret);
    printf("pthread id:%x exitno:%d exittime:%d\n",tid3,((struct Ret*)ret)->exitno,((struct Ret*)ret)->exittime);

    return 0;
}
